package cn.hicard.risk.action.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.hicard.auth.web.AuthFilter;
import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.core.Where;
import cn.hicard.risk.entity.SysNotice;
import cn.hicard.risk.service.sys.NoticeService;
import cn.hicard.util.DateHelp;
import cn.hicard.util.SysLogger;

@SessionAttributes(AuthFilter.LOGON_LOGINID)
@Controller
@RequestMapping("/sys/notice")
public class NoticeAction {
	
	private static SysLogger syslog = SysLogger.getLogger();
	
	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping
	public String search(@RequestParam(required=false,defaultValue="1") int index, 
			@RequestParam(required=false,defaultValue="20") int size, ModelMap map,
			@RequestParam(required=false)String title,
			@RequestParam(required=false)String publisher,
			@RequestParam(required=false)String dateStart,
			@RequestParam(required=false)String dateEnd) throws Exception{
		
		// 查询条件
		Where where=new Where(SysNotice.class);
		where.addLike("title", title);
		where.addEqual("publisher", publisher);
		if(dateStart != null && !dateStart.isEmpty()){
			where.addGreaterEq("publisTime", dateStart+" 00:00:00");
		}
		if(dateEnd != null && !dateEnd.isEmpty()){
			where.addLessEq("publisTime", dateEnd+" 23:59:59");
		}	
		
		// 分页条件，Hibernate查询分页时页码从0开始
		Page page = new Page(index-1, size);		
		PageResult<SysNotice> pr = noticeService.search(where, page);		
		map.addAttribute("list", pr.getDataList());
		map.addAttribute("totalcount", pr.getResultCount());	
		
		return "/sys/notice";
	}
	
	@RequestMapping("/edit")
	public String edit(@RequestParam int pk, ModelMap map) throws Exception{

		map.addAttribute("item", noticeService.getByPk(pk));			
		
		return "/sys/notice";
	}
	
	@RequestMapping("/save")
	public String save(@RequestParam String title, @RequestParam(required=false) Integer pkNotice,
			@RequestParam String content,
			@ModelAttribute(AuthFilter.LOGON_LOGINID) String loginid) throws Exception {
		SysNotice notice = null;
		if(pkNotice != null){
			notice = noticeService.getByPk(pkNotice);
		}
		if(notice == null){
			syslog.logC(loginid, "[系统公告]新增");
			notice = new SysNotice();
			notice.setPublisher(loginid);
			notice.setPublisTime(DateHelp.dateTime());
			notice.setTitle(title);
			notice.setContent(content);
			noticeService.save(notice);
		}else{
			syslog.logU(loginid, "[系统公告]更新");		
			notice.setTitle(title);
			notice.setContent(content);
			notice.setUpdateTime(DateHelp.dateTime());
			noticeService.update(notice);
		}		
		
		return "redirect:/sys/notice.do";
	}
	@RequestMapping("/delete")
	public String delete(@RequestParam int pk, @ModelAttribute(AuthFilter.LOGON_LOGINID) String loginid) throws Exception{
		syslog.logD(loginid, "[系统公告]删除");
		noticeService.delete(pk);
		return "redirect:/sys/notice.do";
	} 

}
